import 'package:flutter/material.dart';
import '../../res.dart';
import '../../utils/Tool/MyTools.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';
import 'package:get/get.dart';

import '../../login/Controller/LoginController.dart';
import '../../utils/Toast/MyTosat.dart';
import '../../utils/color/ColorUtils.dart';
import '../loginregister/ChangePassword.dart';
import '../loginregister/Register.dart';

class MyDialog extends StatefulWidget {
  const MyDialog({Key? key}) : super(key: key);

  @override
  State<MyDialog> createState() => _MyDialogState();
}

class _MyDialogState extends State<MyDialog>
    with SingleTickerProviderStateMixin {
  final Controller controller = Get.put(Controller());
  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();
  late AnimationController _repeatController;

  late Animation<double> _animation;
  bool _isVisible1 = false;
  bool _isVisible2 = false;
  String email = "";
  String password = "";
  bool look = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    /// 会重复播放的控制器
    _repeatController = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    )..repeat();

    /// 线性动画
    _animation = Tween<double>(begin: 0, end: 1).animate(_repeatController);
    controller1.addListener(() {
      ///获取输入的内容
      email = controller1.text;
    });

    controller2.addListener(() {
      ///获取输入的内容
      password = controller2.text;
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _repeatController.stop();
    _repeatController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Container(
        decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.vertical(top: Radius.circular(12))),
        padding: const EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 6),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
              children: [
                Text(
                  "email_and_password".tr,
                  style: TextStyle(
                      fontSize: 18,
                      color: rgba(48, 49, 51, 1),
                      fontWeight: FontWeight.bold),
                ),
                const Spacer(),
                MyLocalImage(
                  Res.actionsheet_icon_close3x,
                  width: 24,
                  height: 24,
                  onclick: () {
                    Get.back();
                  },
                )
              ],
            ),
            const SizedBox(
              height: 16,
            ),
            Container(
              // color: Colors.amber,
              padding: const EdgeInsets.symmetric(horizontal: 16),
              //constraints: const BoxConstraints(maxHeight: 48),
              alignment: Alignment.center,
              child: TextField(
                controller: controller1,
                maxLines: 1,
                onChanged: (v) {
                  setState(() {
                    email = v;
                  });
                },
                decoration: InputDecoration(
                  suffixIcon: Padding(
                    padding: const EdgeInsets.all(14),
                    child: SizedBox(
                      width: 50,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Offstage(
                            offstage: email.isEmpty ? true : false,
                            child: MyLocalImage(
                              Res.sys_icon_delete_grey3x,
                              width: 16,
                              height: 16,
                              onclick: () {
                                controller1.clear();
                                setState(() {
                                  email = "";
                                });
                              },
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                    color: rgba(230, 231, 235, 1),
                    width: 1,
                  )),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: rgba(150, 151, 153, 1), //边框颜色为白色
                      width: 1, //宽度为5
                    ),
                    borderRadius: const BorderRadius.all(
                      Radius.circular(6), //边角为30
                    ),
                  ),
                  hintText: 'email'.tr,
                  hintStyle:
                      TextStyle(color: rgba(200, 202, 204, 1), height: 1),
                ),
              ),
            ),
            Visibility(
              visible: _isVisible1,
              child: Container(
                margin: const EdgeInsets.only(top: 8),
                child: Row(
                  children: [
                    const SizedBox(
                      width: 16,
                    ),
                    Text(
                      "address".tr,
                      style: TextStyle(color: rgba(255, 0, 14, 1)),
                    ),
                    const Spacer()
                  ],
                ),
              ),
            ),
            const SizedBox(
              height: 12,
            ),
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              constraints: const BoxConstraints(maxHeight: 48),
              child: TextField(
                obscureText: look,
                onChanged: (v) {
                  setState(() {
                    password = v;
                  });
                },
                controller: controller2,
                decoration: InputDecoration(
                  suffixIcon: Padding(
                    padding: const EdgeInsets.all(14),
                    child: SizedBox(
                      width: 50,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Offstage(
                            offstage: password.isEmpty ? true : false,
                            child: MyLocalImage(
                              Res.sys_icon_delete_grey3x,
                              width: 16,
                              height: 16,
                              onclick: () {
                                controller2.clear();
                                setState(() {
                                  password = "";
                                });
                              },
                            ),
                          ),
                          MyLocalImage(
                            look
                                ? Res.password_hide_nor
                                : Res.password_display_nor,
                            margin: const EdgeInsets.only(left: 10),
                            width: 20,
                            height: 20,
                            onclick: () {
                              look = !look;
                              setState(() {});
                            },
                          )
                        ],
                      ),
                    ),
                  ),
                  enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: rgba(230, 231, 235, 1))),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: rgba(150, 151, 153, 1), //边框颜色为白色
                      width: 1, //宽度为5
                    ),
                    borderRadius: const BorderRadius.all(
                      Radius.circular(6), //边角为30
                    ),
                  ),
                  hintText: 'password'.tr,
                  hintStyle:
                      TextStyle(color: rgba(200, 202, 204, 1), height: 1),
                ),
              ),
            ),
            Visibility(
              visible: _isVisible2,
              child: Container(
                margin: const EdgeInsets.only(top: 8),
                child: Row(
                  children: [
                    const SizedBox(
                      width: 16,
                    ),
                    Text(
                      "wrong_password".tr,
                      style: TextStyle(color: rgba(255, 0, 14, 1)),
                    ),
                    const Spacer()
                  ],
                ),
              ),
            ),
            const SizedBox(
              height: 24,
            ),
            InkWell(
              onTap: () {
                if (MyTools.stringIsEmpty(email)) {
                  MyToast().show("empty1".tr);
                  return;
                }
                if (MyTools.stringIsEmpty(password)) {
                  MyToast().show("empty2".tr);
                  return;
                }
                controller.Login(email, password);
              },
              child: Container(
                alignment: Alignment.center,
                margin: const EdgeInsets.symmetric(horizontal: 16),
                height: 48,
                decoration: BoxDecoration(
                    color: rgba(255, 78, 77, 1),
                    borderRadius: BorderRadius.circular(6)),
                child: Obx(() => controller.login_ == 1
                    ? RotationTransition(
                        turns: _animation,
                        alignment: Alignment.center,
                        child: const MyLocalImage(
                          Res.tips_icon_loading_white3x,
                          width: 24,
                          height: 24,
                        ),
                      )
                    : Text(
                        'sign_in'.tr,
                        style: const TextStyle(
                            color: Colors.white, fontWeight: FontWeight.bold),
                      )),
              ),
            ),
            // Container(
            //   margin: const EdgeInsets.symmetric(horizontal: 16),
            //   alignment: Alignment.center,
            //   child: MaterialButton(
            //     onPressed: () {
            //       if (MyTools.stringIsEmpty(email)) {
            //         MyToast().show("empty1".tr);
            //         return;
            //       }
            //       if (MyTools.stringIsEmpty(password)) {
            //         MyToast().show("empty2".tr);
            //         return;
            //       }
            //       controller.Login(email, password);
            //     },
            //     minWidth: double.infinity,
            //     color: rgba(255, 78, 77, 1),
            //     height: 48,
            //     shape: RoundedRectangleBorder(
            //       borderRadius: BorderRadius.circular(6),
            //     ),
            //     child: Obx(() => controller.login_ == 1
            //         ? RotationTransition(
            //             turns: _animation,
            //             alignment: Alignment.center,
            //             child: const MyLocalImage(
            //               Res.tips_icon_loading_white3x,
            //               width: 24,
            //               height: 24,
            //             ),
            //           )
            //         : Text(
            //             'sign_in'.tr,
            //             style: const TextStyle(
            //                 color: Colors.white, fontWeight: FontWeight.bold),
            //           )),
            //   ),
            // ),
            const SizedBox(
              height: 12,
            ),
            Row(
              children: [
                Text("have_an_account".tr),
                const Spacer(),
                TextView(
                  "forget_the_password".tr,
                  onclick: () {
                    // setState(() {
                    //   _isVisible1 = true;
                    // });
                    Get.to(const ChangePassword());
                  },
                  style: TextStyle(color: rgba(23, 70, 143, 1)),
                )
              ],
            ),
            Row(
              children: [
                TextView(
                  "register".tr,
                  padding: const EdgeInsets.symmetric(vertical: 5),
                  style: TextStyle(color: rgba(23, 70, 143, 1)),
                  onclick: () {
                    // setState(() {
                    //   _isVisible2 = true;
                    // });
                    Get.to(const Register());
                    //controller.Info();
                  },
                ),
                const Spacer(),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
